package com.android.xunxian.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.android.xunxian.MyApplication;
import com.android.xunxian.R;
import com.android.xunxian.adapter.XunxAdapter;
import com.android.xunxian.entry.Xunx;
import com.android.xunxian.utils.UConstants;
import com.android.xunxian.utils.Util;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.ex.DbException;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by zouqh on 2016/7/16.
 */
public class XunxPlanActivity extends Activity implements View.OnClickListener {


    @InjectView(R.id.close)
    ImageView close;
    @InjectView(R.id.title_text)
    TextView titleText;
    @InjectView(R.id.ritgh_icon)
    ImageView ritghIcon;
    @InjectView(R.id.ritgh_text)
    TextView ritghText;
    ProgressDialog dialog;
    @InjectView(R.id.radio1)
    RadioButton radio1;
    @InjectView(R.id.radio2)
    RadioButton radio2;
    @InjectView(R.id.radio3)
    RadioButton radio3;
    @InjectView(R.id.listview)
    ListView listview;
    @InjectView(R.id.group)
    RadioGroup group;
    private List<Xunx> xunxList;
    private XunxAdapter adapter;//

    private long ccont;
    private String type="执行中";
    public static XunxPlanActivity activity;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xunx_plan_activity);
        ButterKnife.inject(this);
        activity=this;
        titleText.setText("巡线记录");
        ritghText.setText("下载");
        close.setOnClickListener(this);
        ritghText.setOnClickListener(this);
        xunxList = new ArrayList<>();
        try {

            ccont = MyApplication.dbManager.selector(Xunx.class).count();
            if (ccont <= 0) {
//                Toast.makeText(getApplicationContext(), "您还没有下载巡线！", Toast.LENGTH_SHORT).show();
            } else {
                List<Xunx> list = MyApplication.dbManager.selector(Xunx.class).findAll();
                if (list != null && list.size() > 0) {
                    xunxList.addAll(list);

                }
            }

        } catch (DbException e) {
            e.printStackTrace();
        }
        adapter = new XunxAdapter(this, xunxList);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(XunxPlanActivity.this, UpdateFileActivity.class);
                intent.putExtra("name",xunxList.get(position).getNAME());
                intent.putExtra("id",xunxList.get(position).getID());
                startActivity(intent);

            }
        });
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {


                    switch (checkedId) {

                        case R.id.radio1:
                            type="执行中";
                            getDate1();
                            break;
                        case R.id.radio2:
                            type="未开始";
                            getDate1();
                            break;
                        case R.id.radio3:
                            type="已完成";
                            getDate1();
                            break;

                    }

            }
        });
        group.check(R.id.radio1);


    }

    public void getDate1(){
        List<Xunx> list = null;
        try {
            list = MyApplication.dbManager.selector(Xunx.class).findAll();
            xunxList.clear();
            adapter.notifyDataSetChanged();
            if (list != null && list.size() > 0) {
                xunxList.addAll(list);
                adapter.notifyDataSetChanged();
            }
        } catch (DbException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.close:
                finish();
                break;
            case R.id.ritgh_text:
                try {
                    MyApplication.dbManager.saveOrUpdate(xunxList);
                    ritghText.setVisibility(View.GONE);
                } catch (DbException e) {
                    e.printStackTrace();
                }
                break;
        }
    }




}